<template>
  <div style="padding: 20px;height: 670px">
    <t-card style="height: 670px">
      <t-card shadow="never" style="width: 1320px">
        <div class="search-form">
          <!-- 搜索表单 -->
          <el-form inline>
            <el-form-item label="类型">
              <el-select v-model="searchType" placeholder="请选择类型">
                <el-option label="投诉" value="投诉"></el-option>
                <el-option label="意见" value="意见"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="searchStatus" placeholder="请选择状态">
                <el-option label="已接收" value="已接收"></el-option>
                <el-option label="处理中" value="处理中"></el-option>
                <el-option label="已完成" value="已完成"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <t-button theme="primary" size="large" @click="searchFeedbacks()">搜索</t-button>
            </el-form-item>
            <el-form-item>
              <t-button theme="primary" variant="outline" size="large" @click="resetSearch">重置</t-button>
            </el-form-item>
          </el-form>
        </div>
      </t-card>
      <br/>
      <t-card shadow="never" style="width: 1320px">
        <!-- 反馈列表 -->
        <el-table :data="feedbacks" style="height: 400px">
          <el-table-column prop="fbContent" label="内容"></el-table-column>
          <el-table-column prop="fbType" label="类型"></el-table-column>
          <el-table-column prop="owner.ownerName" label="业主姓名"></el-table-column>
          <el-table-column prop="fbTime" label="时间"></el-table-column>
          <el-table-column prop="fbStatus" label="状态">
            <template slot-scope="scope">
              <template v-if="scope.row.isEditing">
                <el-select v-model="scope.row.fbStatus">
                  <el-option label="已接收" value="已接收"></el-option>
                  <el-option label="处理中" value="处理中"></el-option>
                  <el-option label="已完成" value="已完成"></el-option>
                </el-select>
              </template>
              <template v-else>
                {{ scope.row.fbStatus }}
              </template>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <t-button theme="primary" @click="editRow(scope.row)" v-if="!scope.row.isEditing">编辑</t-button>
              <t-button theme="primary" variant="dashed" @click="saveRow(scope.row)" v-else>保存</t-button>
              <el-popconfirm
                  title="确定要删除吗？"
                  confirmButtonText="确定"
                  cancelButtonText="取消"
                  @confirm="deleteFeedback(scope.row)"
                  @cancel="cancelDelete"
              >
                <t-button theme="primary" variant="outline" slot="reference">删除</t-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <br/>
        <!-- 分页 -->
        <el-pagination
            @size-change="handlePageSizeChange"
            @current-change="handlePageChange"
            :page-sizes="[5, 10, 20, 40]"
            :page-size="pageSize"
            :current-page="currentPage"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>

        <!-- Create Form Dialog -->
        <el-dialog :visible="createFormVisible" @close="closeForm">
          <el-form ref="feedbackForm" :model="newFeedback" label-width="80px">
            <el-form-item label="内容" prop="fbContent">
              <el-input v-model="newFeedback.fbContent"></el-input>
            </el-form-item>
            <el-form-item label="类型" prop="fbType">
              <el-input v-model="newFeedback.fbType"></el-input>
            </el-form-item>
            <el-form-item label="业主" prop="ownerId">
              <el-input v-model="newFeedback.ownerId"></el-input>
            </el-form-item>
            <el-form-item label="时间" prop="fbTime">
              <el-date-picker
                  v-model="newFeedback.fbTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="状态" prop="fbStatus">
              <el-input v-model="newFeedback.fbStatus"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取消</el-button>
        <el-button type="primary" @click="createFeedback">确定</el-button>
      </span>
        </el-dialog>

        <!-- Edit Form Dialog -->
        <el-dialog title="处理信息" :visible="editFormVisible" @close="closeForm">
          <el-form ref="editForm" :model="selectedFeedback" label-width="80px">
            <el-form-item label="内容" prop="fbContent">
              <el-input v-model="selectedFeedback.fbContent"></el-input>
            </el-form-item>
            <el-form-item label="类型" prop="fbType">
              <el-select v-model="selectedFeedback.fbType">
                <el-option label="维修" value="维修"></el-option>
                <el-option label="投诉" value="投诉"></el-option>
                <el-option label="意见" value="意见"></el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="业主" prop="owner.ownerName">
              <el-input v-model="selectedFeedback.owner.ownerName"></el-input>
            </el-form-item> -->
            <el-form-item label="时间" prop="fbTime">
              <el-date-picker
                  v-model="selectedFeedback.fbTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :default-time="['00:00:00', '23:59:59']"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="状态" prop="fbStatus">
              <el-select v-model="selectedFeedback.fbStatus">
                <el-option label="已接收" value="已接收"></el-option>
                <el-option label="处理中" value="处理中"></el-option>
                <el-option label="已完成" value="已完成"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取消</el-button>
        <el-button type="primary" @click="updateFeedback">确定</el-button>
      </span>
        </el-dialog>
      </t-card>
    </t-card>
  </div>
</template>

<script>
import axios from 'axios';
import {MessagePlugin} from "tdesign-vue";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'complains',
  data() {
    return {
      feedbacks: [], // 反馈列表
      newFeedback: {
        // 用于新增反馈的对象
        fbContent: '',
        fbType: '',
        ownerId: '',
        fbTime: '',
        fbStatus: ''
      },
      searchStatus: '', // 搜索内容
      searchType: '', // 搜索类型
      selectedFeedback: {}, // 选中的反馈对象，用于编辑和删除
      createFormVisible: false, // 新增表单是否可见
      editFormVisible: false, // 编辑表单是否可见
      pageSize: 5, // 每页显示的数量
      currentPage: 1, // 当前页码
      total: 0// 总记录数
    };
  },
  mounted() {
    this.fetchFeedbacks(); // 页面加载时获取反馈列表
  },
  created() {
    this.searchType = "投诉";
  },
  methods: {

    editRow(row) {
      // console.log(row.isEditing)
      row.isEditing = true;
    },
    saveRow(row) {
      row.isEditing = false;
      const feedback = {...row};
      this.$delete(feedback, 'isEditing');
      axios.post("/api/admin/feedback/updateFb", feedback)
          .then(res => {
                if (res.data.code == 200) {
                  MessagePlugin.success('操作成功')
                  this.fetchFeedbacks();
                }
              }
          )
    },
    cancelDelete() {
    },
    fetchFeedbacks() {
      const pageNum = this.currentPage;
      const pageSize = this.pageSize;
      const feedback = {
        fbType: this.searchType,
        fbStatus: this.searchStatus
      };
      axios
          .post(`/api/admin/feedback/findFb?pageNum=${pageNum}&pageSize=${pageSize}`, feedback)
          .then((response) => {
            this.feedbacks = response.data.data.list.map((feedback) => ({
              ...feedback,
              isEditing: false // 添加isEditing属性，默认为false
            }));
            console.log(this.feedbacks);
            this.total = Number(response.data.data.total);
            //this.ttotal =response.data.data.total;
            console.log(this.total);
          })
          .catch((error) => {
            console.error('Error fetching feedbacks:', error);
          });
    },

    createFeedback() {
      axios
          .post('/api/feedbacks', this.newFeedback)
          .then(() => {
            this.closeForm();
            this.fetchFeedbacks();
          })
          .catch((error) => {
            console.error('Error creating feedback:', error);
          });
    },

    //   updateFeedback() {
    // const feedback={...this.selectedFeedback}
    //     axios
    //       .put(`/api/admin/feedback/updateFb`, feedback)
    //       .then((res) => {
    // 	if(res.data.code==200){
    //         this.closeForm();
    //         this.fetchFeedbacks();
    // 	}
    //       })
    //       .catch((error) => {
    //         console.error('Error updating feedback:', error);
    //       });
    //   },

    deleteFeedback(feedback) {
      axios
          .delete(`/api/admin/feedback/delete?feedbackId=${feedback.fbId}`)
          .then(res => {
            if (res.data.code == 200) {
              MessagePlugin.success('操作成功')
              this.fetchFeedbacks();
            }
          })
          .catch((error) => {
            console.error('Error deleting feedback:', error);
          });
    },

    showCreateForm() {
      this.createFormVisible = true;
    },

    showEditForm(feedback) {
      this.selectedFeedback = {...feedback};
      this.editFormVisible = true;
    },

    closeForm() {
      this.newFeedback = {
        fbContent: '',
        fbType: '',
        ownerId: '',
        fbTime: '',
        fbStatus: ''
      };
      this.selectedFeedback = {};
      this.createFormVisible = false;
      this.editFormVisible = false;
      this.$refs.feedbackForm.resetFields();
      this.$refs.editForm.resetFields();
    },

    searchFeedbacks() {
      this.currentPage = 1;
      this.fetchFeedbacks();


    },

    resetSearch() {
      this.searchStatus = '';
      this.searchType = '投诉';
      this.currentPage = 1;
      this.fetchFeedbacks();
    },

    handlePageSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.currentPage = 1;
      this.fetchFeedbacks();
    },

    handlePageChange(currentPage) {
      this.currentPage = currentPage;
      this.fetchFeedbacks();
    }
  }
};
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
}
</style>
